package com.guanyu.u2cache.model;

import com.guanyu.u2cache.enums.CacheType;
import com.guanyu.u2cache.enums.EventType;
import com.guanyu.u2cache.util.TimeUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * @author zhouyunjian
 * @date 2023/11/14 14:41
 **/
@AllArgsConstructor
@NoArgsConstructor()
@Data
@Builder
public class EventData implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 事件id
     **/
    private String id;
    /**
     * 缓存名列表
     **/
    private String[] cacheNames;
    /**
     * 监听事件名称列表
     **/
    private String[] listenEventNames;
    /**
     * 事件key
     **/
    private String key;
    /**
     * 来源机物理地址
     **/
    private String macAddress;
    /**
     * 事件类型
     **/
    private EventType eventType;
    /**
     * 缓存类型
     **/
    private CacheType cacheType;
    /**
     * 返回类型
     */
    private Class returnType;
    private long expiredTime = 0;
    private TimeUnit expireUnit = TimeUnit.MINUTES;


    /**
     * 提前1/5的时间重载缓存
     *
     * @return
     */
    public long getDelayTime() {
        if (expiredTime <= 0) {
            return 0;
        }
        return TimeUtil.timeTransToSeconds(this.expireUnit, this.expiredTime) * 4 / 5;
    }
}
